﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Company.Database;
using Company.Database.Entities;
using Company.Shell.Interfaces;
using Company.Shell.Services;
using System.Collections.ObjectModel;
using System.Windows.Input;


namespace Company.Shell.ViewModels
{
    public class HistoryAlarmViewmodel : ObservableObject
    {
        /*
         * 字段成员
         * 属性成员
         * ICommand成员
         * 在构造器中获取的实例的成员
         * 构造器
         * 方法生成
         */
        private ObservableCollection<AlarmEntity> alarmEntities = [];
        /// <summary>
        /// 从数据库中查询历史报警记录
        /// </summary>
        public ObservableCollection<AlarmEntity> AlarmEntities
        {
            get => alarmEntities;
            set => SetProperty(ref alarmEntities, value);
        }
        public ICommand LoadedCommand { get; }
        public ICommand SelectRoomCommand { get; }
        public IRoomManager RoomManager { get; }//房间管理器
        private IAlarmRepository AlarmRepository { get; } // 报警记录仓库

        public HistoryAlarmViewmodel(
            IAlarmRepository alarmRepository,
            IRoomManager roomManager)
        {
            RoomManager = roomManager;
            LoadedCommand = new AsyncRelayCommand(OnLoadedCommand);
            SelectRoomCommand = new AsyncRelayCommand(OnLoadedCommand);
            AlarmRepository = alarmRepository;

        }

        private async Task OnLoadedCommand()
        {
            var result = await Task.Run(() => 
            {
                // 查询当前房间的报警记录
                var list = AlarmRepository.GetAll().Where(a => a.RoomNumber == RoomManager.CurrentRoom?.Id).ToList();
                return list;
            });
            AlarmEntities.Clear();
           if (result != null)
           {
               AlarmEntities = new ObservableCollection<AlarmEntity>(result);//将查询结果赋值给属性

            }
       }
   }
}
